package servlet.recipe.control;


import mysql.JdbcUtil;
import search.*;

import java.sql.SQLException;
import java.util.List;

public class Recipe_rsp {
    Search search;
    List<Recipe> recipes;

    public Recipe_rsp(){}

    public List<Recipe> recipes_rsp(int style,String keyword){
        JdbcUtil jdbcUtil=new JdbcUtil();
        try {
            switch (style){
                case 1:
                    //New
                    search=new SearchOfDate();
                    if(jdbcUtil.checkNotNull(keyword)) {
                        recipes = search.recipes(keyword);
                    }else{
                        recipes=search.searchAll();
                    }
                    break;
                case 2:
                    //Hot
                    search=new SearchOfFav();
                    if(jdbcUtil.checkNotNull(keyword)){
                        recipes=search.recipes(keyword);
                    }else{
                        recipes=search.searchAll();
                    }
                    break;
                case 3:
                    //Search
                    search=new SearchOfRecipes();;
                    if(jdbcUtil.checkNotNull(keyword)){
                        recipes=search.recipes(keyword);
                    }else{
                        recipes=search.searchAll();
                    }
                    break;
                case 4:
                    //Recipe
                    search=new SearchRecipes();;
                    if(jdbcUtil.checkNotNull(keyword)){
                        recipes=search.recipes(keyword);
                    }else{
                        recipes=search.searchAll();
                    }
                    break;
                case 5:
                    //Style
                    search=new SearchStyle();;
                    if(jdbcUtil.checkNotNull(keyword)){
                        recipes=search.recipes(keyword);
                    }else{
                        recipes=search.searchAll();
                    }
                    break;
                default:
                    search=new SearchOfRecipes();
                    recipes=search.searchAll();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        return recipes;
    }
    public List<Recipe> cheackRecipesNull(List<Recipe> recipes){
        if (recipes.isEmpty()){
            return this.recipes_rsp(0,null);
        }
        return recipes;

    }
}
